<?php
/*
  功能：平台用户角色关联 model
  作者：cxm
  时间：2019-01-11
*/

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class AdminUserRole extends BaseModel {

    protected $table='admin_user_role';  //绑定表

    /**
     * 权限
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function role(){
        return $this->hasOne(AdminRole::class,'id','role_id');
    }

    /**
     * 编辑
     * @param \Closure|string $where
     * @param $field
     * @return mixed
     */
    public static function updated($where=array(),$field=array()){
        return self::where($where)->update($field);
    }

    /**
     * 查询详情
     * @param $where
     * @return mixed
     */
    public static function detail($where){
        return self::where($where)->first();
    }

    /**
     * 根据管理账号获取所属角色
     * @param $user_id int
     * @return string
     */
    public static function byUserIdGetRoleId($user_id)
    {
        $role_id = self::where('user_id', $user_id)->value('role_id');
        return $role_id ?: 0;
    }

}